/*
 * Este programa es software libre; usted puede redistribuirlo y/o modificarlo bajo los terminos
 * de la licencia "GNU General Public License" publicada por la Fundacion "Free Software Foundation".
 * Este programa se distribuye con la esperanza de que pueda ser util, pero SIN NINGUNA GARANTIA;
 * vea la licencia "GNU General Public License" para obtener mas informacion.
 */
package adalid.core;

import adalid.core.interfaces.PersistentEntity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang.StringUtils;

/**
 * @author Jorge Campins
 */
public class Report extends AbstractArtifact implements Comparable<Report> {

    private PersistentEntity _entity;

    private int _interFieldGapWidth;

    private List<ReportGroup> _groups;

    private ReportGroup _detailGroup;

    private static final int COLUMN_WIDTH = 960;

    public Report(String name) {
        if (name != null) {
            setDeclared(name);
        }
    }

    public Report(PersistentEntity entity) {
        if (entity != null) {
            _entity = entity;
            _groups = new ArrayList<>();
            ReportGroup _reportGroup;
            _reportGroup = new ReportGroup(this, 1);
            _reportGroup = new ReportGroup(this, 2);
            _reportGroup = new ReportGroup(this, 3);
            _reportGroup = new ReportGroup(this, 4);
            _detailGroup = new ReportGroup(this);
            List<ReportField> fields = _detailGroup.getFields();
            if (fields != null && fields.size() > 0) {
                resize(_detailGroup);
            }
            setDeclared(entity.getName());
        }
    }

    /**
     * @return the entity
     */
    public PersistentEntity getEntity() {
        return _entity;
    }

    /**
     * @param entity the entity to set
     */
    void setEntity(PersistentEntity entity) {
        _entity = entity;
    }

    /**
     * @return the inter field gap width
     */
    public int getInterFieldGapWidth() {
        return _interFieldGapWidth;
    }

    /**
     * @param interFieldGapWidth the inter field gap width to set
     */
    void setInterFieldGapWidth(int interFieldGapWidth) {
        _interFieldGapWidth = interFieldGapWidth;
    }

    /**
     * @return the groups list
     */
    @SuppressWarnings("unchecked")
    public List<ReportGroup> getGroups() {
        List list = (List) _groups;
        Collections.sort(list);
        return list;
    }

    /**
     * @param groups the groups list to set
     */
    void setGroups(List<ReportGroup> groups) {
        _groups = groups;
    }

    /**
     * @return the detail group
     */
    public ReportGroup getDetailGroup() {
        return _detailGroup;
    }

    private void resize(ReportGroup group) {
        int used = 0;
        int size = 0;
        ReportField last = null;
        for (ReportField field : group.getFields()) {
            if (field.getPixels() > 0 && field.getPixels() + used <= COLUMN_WIDTH) {
                used += field.getPixels();
                if (field.isResizeable()) {
                    last = field;
                    size += field.getPixels();
                }
            }
        }
        int free = COLUMN_WIDTH - used;
        if (last != null && free > 0) {
            int extra;
            for (ReportField field : group.getFields()) {
                if (field.getPixels() > 0 && used < COLUMN_WIDTH) {
                    if (field == last) {
                        extra = COLUMN_WIDTH - used;
                    } else if (field.isResizeable()) {
                        extra = field.getPixels() * free / size;
                    } else {
                        extra = 0;
                    }
                    if (extra > field.getPixels()) {
                        extra = field.getPixels();
                    }
                    used += extra;
                    field.setPixels(field.getPixels() + extra);
                }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Comparable">
    @Override
    public int compareTo(Report o) {
        Report that;
        if (o != null) {
            that = o;
            String thisName = StringUtils.trimToEmpty(this.getName());
            String thatName = StringUtils.trimToEmpty(that.getName());
            return thisName.compareTo(thatName);
        }
        return 0;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="toString">
    @Override
    protected String fieldsToString(int n, String key, boolean verbose, boolean fields, boolean maps) {
        String tab = verbose ? StringUtils.repeat(" ", 4) : "";
        String fee = verbose ? StringUtils.repeat(tab, n) : "";
        String faa = " = ";
        String foo = verbose ? EOL : ", ";
        String string = super.fieldsToString(n, key, verbose, fields, maps);
        if (fields || verbose) {
            if (verbose) {
                if (_entity != null) {
                    string += fee + tab + "entity" + faa + _entity + foo;
                }
            }
        }
        return string;
    }
    // </editor-fold>
}
